package top.dreamcenter.rss.rsssolverweb.controller;

import org.springframework.web.bind.annotation.*;
import top.dreamcenter.rss.rsssolverweb.entity.KVPair;
import top.dreamcenter.rss.rsssolverweb.entity.PluginInfo;
import top.dreamcenter.rss.rsssolverweb.entity.ReqModel;
import top.dreamcenter.rss.rsssolverweb.entity.RetResult;
import top.dreamcenter.rss.rsssolverweb.util.RedisHelper;
import top.dreamcenter.rss.rsssolverweb.util.SpiderHelper;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/plugin")
public class ManagePluginController {

    @Resource
    private Map<String, SpiderHelper> spiders;

    @Resource
    private RedisHelper redisHelper;

    @GetMapping("/list/plugin")
    public RetResult<List<PluginInfo>> listPlugins() {
        List<PluginInfo> list = new ArrayList<>();

        spiders.forEach((k, v) -> list.add(new PluginInfo(
                k,
                v.getRoute(),
                v.getCron(),
                v.listKey().entrySet().stream()
                        .map((item -> new KVPair(item.getKey(), item.getValue())))
                        .collect(Collectors.toList()),
                v.listConfig()
                        .entrySet().stream()
                        .map((item -> new KVPair(item.getKey(), item.getValue())))
                        .collect(Collectors.toList()),
                v.getLastRunAt(),
                v.getLastRunResult()
        )));

        return RetResult.success(list);
    }

    @GetMapping("/list/key")
    public RetResult<List<KVPair>> listKeys(String pluginId) {
        if (spiders.containsKey(pluginId))  {
            Hashtable<String, String> table = spiders.get(pluginId).listKey();
            List<KVPair> kvPairList = table.entrySet().stream()
                    .map((item -> new KVPair(item.getKey(), item.getValue())))
                    .collect(Collectors.toList());
            return RetResult.success(kvPairList);
        } else {
            return RetResult.fail(pluginId + " 未注册");
        }
    }

    @GetMapping("/list/config")
    public RetResult<List<KVPair>> listPlugins(String pluginId) {
        if (spiders.containsKey(pluginId))  {
            Hashtable<String, String> table = spiders.get(pluginId).listConfig();
            List<KVPair> kvPairList = table.entrySet().stream()
                    .map((item -> new KVPair(item.getKey(), item.getValue())))
                    .collect(Collectors.toList());
            return RetResult.success(kvPairList);
        } else {
            return RetResult.fail(pluginId + " 未注册");
        }
    }

    @PostMapping("/add/key")
    public RetResult<String> addKey(@RequestBody ReqModel model) {
        SpiderHelper spiderHelper = spiders.get(model.getPluginId());
        spiderHelper.addKey(model.getK(), model.getV());

        redisHelper.addHashResult(spiderHelper.getRoute() + "-key", model.getK(), model.getV());

        return RetResult.success("添加key结束");
    }

    @PostMapping("/add/config")
    public RetResult<String> addConfig(@RequestBody ReqModel model) {
        SpiderHelper spiderHelper = spiders.get(model.getPluginId());
        spiderHelper.addConfig(model.getK(), model.getV());

        redisHelper.addHashResult(spiderHelper.getRoute() + "-config", model.getK(), model.getV());

        return RetResult.success("添加config结束");
    }

    @PostMapping("/del/key")
    public RetResult<String> delKey(@RequestBody ReqModel model) {
        if (spiders.containsKey(model.getPluginId()))  {
            SpiderHelper spiderHelper = spiders.get(model.getPluginId());
            spiderHelper.delKey(model.getK());

            redisHelper.delHashResult(spiderHelper.getRoute() + "-key", model.getK());

            return RetResult.success("删除key结束");
        } else {
            return RetResult.fail(model.getPluginId() + " 未注册");
        }
    }

    @PostMapping("/del/config")
    public RetResult<String> delConfig(@RequestBody ReqModel model) {
        if (spiders.containsKey(model.getPluginId()))  {
            SpiderHelper spiderHelper = spiders.get(model.getPluginId());
            spiderHelper.delConfig(model.getK());

            redisHelper.delHashResult(spiderHelper.getRoute() + "-config", model.getK());

            return RetResult.success("删除config结束");
        } else {
            return RetResult.fail(model.getPluginId() + " 未注册");
        }
    }
}
